<?php
class ContentController extends CBaseController
{
	
	protected $content;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
	}
	
	public function all()
	{
	 $contents = R::findAll('content',' ORDER BY name');
	 $this->templator->assign('contents',array_merge($contents, array()));
	 $this->templator->display('content_list.html');
	}
	
	public function savePrepare()
	{
		$this->content = R::dispense('content');
		$this->content->name = $_POST['name'];
		$this->content->header = $_POST['header'];
		$this->content->body = $_POST['body'];
		$this->content->footer = $_POST['footr'];
		$this->content->type = $_POST['type'];		
		$this->content->title = $_POST['title'];
	}
	
	public function updatePrepare()
	{
		$id = $_POST['id'];
		$this->content = R::load('content',$id);
		$this->content->name = $_POST['name'];
		$this->content->header = $_POST['header'];
		$this->content->body = $_POST['body'];
		$this->content->footer = $_POST['footr'];		
		$this->content->type = $_POST['type'];		
		$this->content->title = $_POST['title'];		
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->content = R::dispense('content');
		$this->templator->assignByRef('content',$this->content);
		$this->templator->display('content_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->content);
		$this->templator->assign('info','Content added sucessfully');
		$this->templator->assignByRef('content',$this->content);		
		$this->templator->display('content_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('content',$this->content);
		$this->templator->display('content_general.html');
	  }
	}
	
	public function edit()
	{
		$id = $_GET['id'];
		if(isset($id))
		$this->content = R::load('content',$id);
		else
		$this->content = R::dispense('content');
		$this->templator->assignByRef('content',$this->content);		
		$this->templator->display('content_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->content);
			$this->templator->assignByRef('content',$this->content);
			$this->templator->assign('info','Content updated sucessfully');
			$this->templator->display('content_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('content',$this->content);
			$this->templator->display('content_general.html');
		  }
	}
	
	public function delete()
	{
	 $id = $_GET['id'];
	 if(isset($id))
	 {
		$content = R::load('content',$id);
		if(isset($content))
		{
			R::trash($content);
			$this->templator->assign('info','Content deleted sucessfully');
		}
	 }
	 $this->all();
	}
}	